<?php
class Technorati extends BlockInstance {
	public $CI;
	
	function Technorati()
	{
		$this->CI =& get_instance();
	}
	
	function __call($m, $a)
	{
		$content = $a[1];
		
		// search Technorati for blog posts containing the entry title
		// http://technorati.com/developers/api/search.html
		if (strlen($content->field1_value))
		{
			$api_key = "43d3f762198375ba4d07442b4ef5fa98";
			
			$terms = h(xml_convert($content->field1_value));
			$ch    = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://api.technorati.com/search?key={$api_key}&query=%22{$terms}%22");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'text/xml; charset=utf-8'));
			$ret = curl_exec($ch);
			curl_close($ch);
			
			// response xml is in $ret
			$xml = new SimpleXMLElement($ret, LIBXML_NOERROR);
			$data['xml'] = $xml;
			$this->CI->load->view('blocks/technorati/content', $data);
		}
		else
		{
			echo 'No URL found for this entry.';
		}
	}
}
?>